package com.zhiwang.planet.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity.CENTER
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.zhiwang.planet.R
import com.zhiwang.planet.util.dp
import kotlinx.android.synthetic.main.tool_text_layout.view.*

class ToolTextView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {


    private var text: String?
    private var textColor: Int
    private var textSize: Float
    private var drawableTop: Drawable?

    init {

        val ta = context.obtainStyledAttributes(attrs, R.styleable.ToolTextView)

        textColor = ta.getColor(R.styleable.ToolTextView_tt_textColor, Color.BLACK)
        textSize = ta.getDimensionPixelSize(R.styleable.ToolTextView_tt_textSize, 13.dp).toFloat()
        text = ta.getString(R.styleable.ToolTextView_tt_text)
        drawableTop = ta.getDrawable(R.styleable.ToolTextView_tt_drawableTop)
        ta.recycle()


        val rootView = LayoutInflater.from(context).inflate(R.layout.tool_text_layout, null)


        val toolTextview = rootView.tool_textview
        toolTextview.text = text
        toolTextview.setTextColor(textColor)
        toolTextview.textSize = textSize/resources.displayMetrics.scaledDensity
        toolTextview.setCompoundDrawablesRelativeWithIntrinsicBounds(
                toolTextview.compoundDrawables[0],
                drawableTop,
                toolTextview.compoundDrawables[2],
                toolTextview.compoundDrawables[3])

        val lp = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        lp.gravity = CENTER
        addView(rootView, lp)
    }

}